var util = require('../../utils/util');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import { $wuxLoading } from '../../dist/index'

var app = getApp();
var that;
var pageInex = 1;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    price : 0,
    gift:0,
    show: false,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	util.loading = $wuxLoading();
    that = this;
    util.showLoading();
    app.getUserInfo()
    .then(res => {
      return wxRequest.postRequest(config.chargeList, {});
    })
    .then(res => {
      var list= res.list;
      for(var i in list){
        list[i].recharge_amount= list[i].recharge_amount /100;
        list[i].gift_amount= list[i].gift_amount /100;
      }
      that.setData({
        list: list
      })
              
    })
    .catch(res => {
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });

  },


  goPay: function () {
    var params = {};
    params.amount =this.data.price;
    params.gift = this.data.gift;
    // if (params.amount<1){
    //     return false;
    // }

    wxRequest.postRequest(config.doCharge, params)
    .then(res => {
        wx.requestPayment({
          'appId': res.appId,
          'timeStamp': res.timeStamp + "",
          'nonceStr': res.nonceStr,
          'package': 'prepay_id='+res.prepay_id,
          'signType': 'MD5',
          'paySign': res.paySign,
          'success': function (res) {
            wx.showToast({
              title: "充值成功",
              icon: 'success',
              duration: 3000,
              mask: true
            });
            setTimeout(function () {
              wx.switchTab({
                url: '../../pages/my/index',
              })
            }, 3000) 
          },
          'fail': function (res) {
            console.log(res);
          }
        })
    })
    .catch(res => {
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });

  },
  chose: function (optioins) {
    var changePrice = optioins.target.id;
    var gift = optioins.target.dataset.myGift;
    this.setData({
      show: true,
      price: changePrice ,
      gift:gift
    });
    console.log(this.data);
  },


})